﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.Diagnostics;

namespace COFE.Core.Utils
{
    /// <summary>
    /// Entry related Utils.
    /// </summary>
    public static class EntryInternalUtils
    {
        /// <summary>
        /// Copy, always expand File based file-system.
        /// </summary>
        public static void Copy(IInternalFileSystemInfoExA entry, IInternalDirectoryInfoExA dir)
        {
            dir.DirectoryController.Copy(entry, dir, true);
        }

        /// <summary>
        /// Move, always expand File based file-system.
        /// </summary>
        public static void Move(IFileSystemInfo entry, IDirectoryInfo dir)
        {
            Move(entry as IInternalFileSystemInfoExA, dir as IInternalDirectoryInfoExA);
        }

        /// <summary>
        /// Move, always expand File based file-system.
        /// </summary>
        public static void Move(IInternalFileSystemInfoExA entry, IInternalDirectoryInfoExA dir)
        {
            dir.DirectoryController.Move(entry, dir, true);
        }

        /// <summary>
        /// Copy, always expand File based file-system.
        /// </summary>
        public static void Copy(IFileSystemInfo entry, IDirectoryInfo dir)
        {
            Copy(entry as IInternalFileSystemInfoExA, dir as IInternalDirectoryInfoExA);
        }

        /// <summary>
        /// Convert entries list's ParseNames to a list of strings.
        /// </summary>
        /// <param name="entryList"></param>
        /// <returns></returns>
        public static string[] ToStringList(this IFileSystemInfo[] entryList)
        {
            string[] retVal = new string[entryList.Length];
            for (int i = 0; i < entryList.Length; i++)
                retVal[i] = entryList[i].ParseName;
            return retVal;
        }



        #region GetRelativePath
        /// <summary>
        /// Get relative path of a entries based on baseDirectory.
        /// e.g. C:\Temp\AbC\1.txt (entries), C:\Temp\ (baseDirectory) will return ABC\1.txt
        /// </summary>
        /// <param name="entries"></param>
        /// <param name="baseDirectory"></param>
        /// <returns></returns>
        public static string GetRelativePath(this IFileSystemInfo entry, IDirectoryInfo baseDirectory)
        {
            if (entry.Equals(baseDirectory))
                return "";

            if (entry.ParseName.IndexOf(baseDirectory.ParseName, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                if (Debugger.IsAttached)
                    Debugger.Break();
                return entry.Name;
            }
            else return entry.ParseName.Substring(baseDirectory.ParseName.Length + 1);
        }

        /// <summary>
        /// Get relative path of a entries based on baseDirectory.
        /// e.g. C:\Temp\AbC\1.txt (entries), C:\Temp\ (baseDirectory) will return ABC\1.txt
        /// </summary>
        /// <param name="entries"></param>
        /// <param name="baseDirectory"></param>
        /// <returns></returns>
        public static string GetRelativePath(this string name, IDirectoryInfo baseDirectory)
        {
            if (name.IndexOf(baseDirectory.ParseName, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                if (Debugger.IsAttached)
                    Debugger.Break();
                return COFEPath.GetFileName(name);
            }
            else return name.Substring(baseDirectory.ParseName.Length + 1);
        }
        #endregion

    }
}
